package com.atxuwei.yygh.order.controller.api;

import com.atxuwei.yygh.common.result.Result;
import com.atxuwei.yygh.common.utils.AuthContextHolder;
import com.atxuwei.yygh.enums.OrderStatusEnum;
import com.atxuwei.yygh.model.order.OrderInfo;
import com.atxuwei.yygh.order.service.OrderService;
import com.atxuwei.yygh.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {
    @Autowired
    private OrderService orderService;

    //生成挂号订单
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrder(@PathVariable String scheduleId,@PathVariable Long patientId){
        Long orderId=orderService.saveOrder(scheduleId,patientId);
        return Result.ok(orderId);
    }

    //根据订单id查询订单详情
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId){
        OrderInfo orderInfo =orderService.getOrders(orderId);
        return Result.ok(orderInfo);
    }


    //查询用户所有预约订单
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page, @PathVariable Long limit, OrderQueryVo orderQueryVo, HttpServletRequest request) {
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderService.selectPage(pageParam, orderQueryVo);
        return Result.ok(pageModel);
    }


    //查询所有订单状态
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }


    //完成支付后，更新订单状态
    @GetMapping("auth/updateOrder/{orderId}")
    public Result updateOrder(@PathVariable Long orderId){
        orderService.updateOrder(orderId);
        return Result.ok();
    }

    //取消预约订单
    @GetMapping("auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable Long orderId){
        boolean isOrder=orderService.cancelOrder(orderId);
        return Result.ok(isOrder);
    }

}
